package com.go.where.family.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.go.where.family.dao.BaseDao;
import com.go.where.family.utils.DateUtil;

public abstract class BaseDaoImpl<T, PK extends Serializable> extends
		SqlSessionDaoSupport implements BaseDao<T, String> {

	public String getPrimaryKey() {
		String dayStr = DateUtil
				.getCurrentDateStr(DateUtil.C_DATA_PATTON_YYYYMMDD);
		StringBuilder sb = new StringBuilder(dayStr);
		String next = getNextKey();
		sb.append(String.format("%08d", next));
		return sb.toString();
	}

	public String getNextKey() {
		String next = getSqlSession().selectOne(
				this.getNameSpace() + ".getNextKey");
		return next;
	}

	public T queryByPrimaryKey(T t) {
		StringBuilder queryId = new StringBuilder(getNameSpace());
		queryId.append(".").append("selectByPrimaryKey");
		return getSqlSession().selectOne(queryId.toString(), t);
	}

	public int delete(T t) {
		return getSqlSession().delete(getNameSpace() + ".delete", t);
	}

	public int update(T t) {
		return getSqlSession().update(getNameSpace() + ".update", t);
	}

	public int insert(T t) {
		return getSqlSession().insert(getNameSpace() + ".insert", t);
	}

	/**
	 * 分页查询
	 * 
	 * @param paramMap
	 * @return
	 */
	public List<T> queryT(Map<String, Object> paramMap) {
		return getSqlSession().selectList(getNameSpace() + ".queryT",
				paramMap);

	}

	/**
	 * 有关联查询时分页查询
	 * 
	 * @param paramMap
	 * @return
	 */
	public List<Map<String, Object>> queryMap(Map<String, Object> paramMap) {
		return getSqlSession().selectList(getNameSpace() + ".queryMap",
				paramMap);
	}

	/**
	 * 查询记录数
	 * 
	 * @param paramMap
	 * @return
	 */
	public Integer queryCount(Map<String, Object> paramMap) {
		return getSqlSession().selectOne(getNameSpace() + ".queryCount",
				paramMap);
	}

	/**
	 * 不分页
	 * 
	 * @param t
	 * @return
	 */
	public List<T> query(Map<String, Object> paramMap) {
		return this.getSqlSession().selectList(getNameSpace() + ".query",
				paramMap);
	}

	public List<T> query() {
		return this.getSqlSession().selectList(getNameSpace() + ".query");
	}
}
